﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    BusinessLogicReference.BusinessLogicClient blClient = new BusinessLogicReference.BusinessLogicClient();
    protected void Page_Load(object sender, EventArgs e)
    {
        //BusinessLogicReference.BusinessLogicClient BusinessLogic = new BusinessLogicReference.BusinessLogicClient();
        //Label1.Text = BusinessLogic.GetData(151);
        
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        // code to log in a user if the user that comes back has an ID of 0
        // the login is invalid so display error message
        // else set session variables and send on to Home page (Console page during testing)
        BusinessLogicReference.UserDetails user = new BusinessLogicReference.UserDetails();
        user.UserName = txtUserName.Text.ToString();
        user.UserKey = txtPassword.Text.ToString();
        user = blClient.LogIn(user);
        if(user.UserID == 0)
        {
            loginError.Text = "UserName or Password is Invalid";
        }
        else
        {
            Session["UserName"] = user.UserName;
            Session["UserID"] = user.UserID;
            Session["UserKey"] = user.UserKey;
            Session["Email"] = user.Email;
            Response.Redirect("Home.aspx");
        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        // Check that passwords are the same if not display error
        // Check that email is filled out if not display error
        // Check that user name is filled out if not display error
        // try to register user and display returned string.
        if (!txtRegPass.Text.Equals(txtRegRePass.Text.ToString()) || txtRegPass.Text.Equals(""))
        {
            regError.Text = "Passwords do not Match";
        }
        else if (!IsValidEmail(txtRegEmail.Text))
        {
            regError.Text = "Please enter a valid Email Address";
        }
        else if (txtRegUser.Text.Equals(""))
        {
            regError.Text = "Please Choose a User Name";
        }
        else
        {
            BusinessLogicReference.UserDetails user = new BusinessLogicReference.UserDetails();
            user.UserName = txtRegUser.Text.ToString();
            user.UserKey = txtRegPass.Text.ToString();
            user.Email = txtRegEmail.Text.ToString();
            regError.Text = blClient.Register(user);

        }

    }

    // imported code for checking if email is valid goto:
    // http://msdn.microsoft.com/en-us/library/01escwtf(v=vs.110).aspx
    // for more info
    bool invalid = false;
    public bool IsValidEmail(string strIn)
    {
        if (String.IsNullOrEmpty(strIn))
            return false;

        // Use IdnMapping class to convert Unicode domain names. 
        try
        {
            strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
                                  RegexOptions.None, TimeSpan.FromMilliseconds(200));
        }
        catch (RegexMatchTimeoutException)
        {
            return false;
        }

        if (invalid)
            return false;

        // Return true if strIn is in valid e-mail format. 
        try
        {
            return Regex.IsMatch(strIn,
                  @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                  @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,24}))$",
                  RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
        }
        catch (RegexMatchTimeoutException)
        {
            return false;
        }
    }
    private string DomainMapper(Match match)
    {
        // IdnMapping class with default property values.
        IdnMapping idn = new IdnMapping();

        string domainName = match.Groups[2].Value;
        try
        {
            domainName = idn.GetAscii(domainName);
        }
        catch (ArgumentException)
        {
            invalid = true;
        }
        return match.Groups[1].Value + domainName;
    }
}